/**
 * 
 */
package l1j.opqlo.假人系統;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javolution.util.FastMap;
import javolution.util.FastTable;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.DatabaseFactory;
import l1j.server.server.ActionCodes;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1DummyInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1NewDummyInstance;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.SQLUtil;

/**
 * @author user
 *
 */
public class A_DummyTable
{
	private final static Logger _log = Logger.getLogger(A_DummyTable.class.getName());
	private static A_DummyTable _instance;
	
	public static A_DummyTable get(){
		if(_instance == null){
			_instance = new A_DummyTable();
		}
		return _instance;
	}

	private HashMap<String , L1DummyInstance>_dummyList = new HashMap<String , L1DummyInstance>();
	
//	private String[] spawnLocation;
//	private String[] titles;
//	private String[] shopBuyItem;
//	private String[] shopSellItem;
//	private String[] name;
	
	public final void loadNpc(){
		
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try
		{
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM z_npc_shop");
			rs = pstm.executeQuery();
			
			while (rs.next()){
				L1Character cha; // 角色的底層物件
				byte action = rs.getByte("action"); // 取得角色目前的動作
				
				// 當動作為個人商店時
				if (action == ActionCodes.ACTION_Shop){
					cha = new L1PcInstance();
				}else{
					cha = new L1NewDummyInstance();
					L1NewDummyInstance dummy = (L1NewDummyInstance) cha;
					dummy.setShopId(rs.getInt("id"));
					dummy.setConsume(rs.getInt("consume"));
					dummy.setGuiType(rs.getShort("shop_gui_type"));
					dummy.setShopHtml(rs.getString("shop_html"));
				}
				
				cha.setName(rs.getString("name"));
				cha.setTitle(rs.getString("title"));
				cha.setGfxId(rs.getShort("gfxid"));
				cha.setStatus(action);
				cha.setOwnLightSize(rs.getByte("bright"));
				cha.setLevel(rs.getByte("level"));
				cha.setMap(rs.getShort("map"));
				cha.setX(rs.getInt("x"));
				cha.setY(rs.getInt("y"));
			}
		}
		catch (SQLException e)
		{
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		finally
		{
			SQLUtil.close(rs, pstm, con);
		}
	}
	
//	public final void settingDummy()
//	{
//		
//	}
//	
//	public final void loadAmount()
//	{
//		FileInputStream fis = new FileInputStream(new File("./data/DummyAmount.ini"));
//		
//		try
//		{
//			fis.loadFile();
//		}
//		catch (IOException e)
//		{
//		}
//		
//		this.spawnLocation = fis.getText().split("\r\n");
//	}
//	
//	public final void loadSpawnLocation()
//	{
//		FileInputStream fis = new FileInputStream(new File("./data/DummySpawnLocation.ini"));
//		
//		try
//		{
//			fis.loadFile();
//		}
//		catch (IOException e)
//		{
//		}
//		
//		this.spawnLocation = fis.getText().split("\r\n");
//	}
//	
//	public final void loadTitle()
//	{
//		FileInputStream fis = new FileInputStream(new File("./data/DummyTitle.ini"));
//		
//		try
//		{
//			fis.loadFile();
//		}
//		catch (IOException e)
//		{
//		}
//		
//		this.titles = fis.getText().split("\r\n");
//	}
//	
//	public final void loadBuyItem()
//	{
//		FileInputStream fis = new FileInputStream(new File("./data/DummyShopBuyItem.ini"));
//		
//		try
//		{
//			fis.loadFile();
//		}
//		catch (IOException e)
//		{
//		}
//		
//		this.shopBuyItem = fis.getText().split("\r\n");
//	}
//	
//	public final void loadShopSellItem()
//	{
//		FileInputStream fis = new FileInputStream(new File("./data/DummyShopSellItem.ini"));
//		
//		try
//		{
//			fis.loadFile();
//		}
//		catch (IOException e)
//		{
//		}
//		
//		this.shopSellItem = fis.getText().split("\r\n");
//	}
//	
//	public final void loadName()
//	{
//		FileInputStream fis = new FileInputStream(new File("./data/DummyName.ini"));
//		
//		try
//		{
//			fis.loadFile();
//		}
//		catch (IOException e)
//		{
//		}
//		
//		this.name = fis.getText().split("\r\n");
//	}
//	
//	/**
//	 * @return the spawnLocation
//	 */
//	public final String[] getSpawnLocation()
//	{
//		return spawnLocation;
//	}
//
//	/**
//	 * @return the titles
//	 */
//	public final String[] getTitles()
//	{
//		return titles;
//	}
//
//	/**
//	 * @return the shopBuyItem
//	 */
//	public final String[] getShopBuyItem()
//	{
//		return shopBuyItem;
//	}
//
//	/**
//	 * @return the shopSellItem
//	 */
//	public final String[] getShopSellItem()
//	{
//		return shopSellItem;
//	}
//
//	/**
//	 * @return the name
//	 */
//	public final String[] getName()
//	{
//		return name;
//	}
	
	public final void load()
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		try
		{
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM z_npc_chat");
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				int id = rs.getInt("npcid");
				String chat = rs.getString("chat");
				String title = rs.getString("title");
				
				for (L1DummyInstance shop : L1World.getInstance().getDummyList())
				{
					if (shop.getNpcId() == id)
					{
						shop.setShopName(chat);
						shop.setTitle(title);
					}
				}
			}
		}catch (SQLException e){
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}finally{
			SQLUtil.close(rs, pstm, con);
		}
	}
	
	private final FastMap<Integer, FastTable<L1Dummy>> sellList = new FastMap<Integer, FastTable<L1Dummy>>();
	private final FastTable<Integer> buyList = new FastTable<Integer>();
	
	public final void load_shop()
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		sellList.clear();
		
		try{
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM z_npc_shop_sell");
			rs = pstm.executeQuery();
			
			while (rs.next()){
				int npcid = rs.getInt("npcid");
				
				if (!sellList.containsKey(npcid)){
					sellList.put(npcid, new FastTable<L1Dummy>());
				}
				
				final int itemid = rs.getInt("itemid");
				L1Item item =ItemTable.getInstance().getTemplate(itemid);
				if(item == null){
					_log.info("假人道具編號: "+itemid+" 不存在。");
					continue;
				}
				L1Dummy ns = new L1Dummy();
				ns.setId(rs.getInt("id"));
				ns.setItem(new L1ItemInstance(item, 1));
				ns.getItem().setIdentified(true);
				ns.setSafeenchant(rs.getInt("safenchant"));
				ns.getItem().setEnchantLevel(ns.getSafeenchant());
				ns.setCount(rs.getLong("count"));
				ns.setMaxCount(rs.getLong("maxcount"));
				ns.getItem().setCount(ns.getCount());
				ns.setMoney(rs.getLong("money"));
				ns.setDayTime(rs.getInt("day_time"));
				
				if ((npcid >= 97302 && npcid <= 97313) || buyList.contains(ns.getItem().getItemId()))
				{
					buyList.add(ns.getItem().getItemId());
				}
				
				sellList.get(npcid).add(ns);
			}
		}
		catch (SQLException e){
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}finally{
			SQLUtil.close(rs, pstm, con);
		}
	}

	/**
	 * @return the sellList
	 */
	public final FastMap<Integer, FastTable<L1Dummy>> getSellList()
	{
		return sellList;
	}

	/**
	 * @return buyList
	 */
	public final FastTable<Integer> getBuyList()
	{
		return buyList;
	}
}
